package com.teamwhole.modules.oa.facility.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.github.pagehelper.Page;
import com.google.common.collect.Maps;
import com.teamwhole.common.model.ResultInfo;
import com.teamwhole.modules.oa.facility.entity.WorkOrderFacility;
import com.teamwhole.modules.oa.facility.service.WorkOrderFacilityService;
import com.teamwhole.modules.sys.utils.UserUtils;

@Component
@Path("/oa/facility/workorder")
@Consumes("application/json;charset=utf-8")
@Produces("application/json;charset=utf-8")
public class WorkOrderFacilityResource {

	@Autowired
	private WorkOrderFacilityService wos;

	@Path("list")
	@POST
	public Object list(WorkOrderFacility entity) {
		// entity.setCreator(UserUtils.getUser());

		String sdate1 = "";
		try {
			sdate1 = entity.getParamMap().get("date1").toString();
			if (sdate1 != "")
				sdate1 = sdate1 + " 00:00:00";
		} catch (Exception e) {
			sdate1 = "";
		}
		String sdate2 = "";
		try {
			sdate2 = entity.getParamMap().get("date2").toString();
			if (sdate2 != "")
				sdate2 = sdate2 + " 00:00:00";
		} catch (Exception e) {
			sdate2 = "";
		}

		Date date1 = null;
		Date date2 = null;

		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			if (sdate1 != "")
				date1 = sdf.parse(sdate1);
			entity.getParamMap().put("date1", date1);
			if (sdate2 != "") {
				date2 = sdf.parse(sdate2);
				Calendar c = Calendar.getInstance();
				c.setTime(date2);
				c.add(Calendar.HOUR, 24);
				date2 = c.getTime();
				entity.getParamMap().put("date2", date2);
			}

		} catch (ParseException e) {
			;// e.printStackTrace();
		}

		//return wos.findPage(entity);
		
		Page<WorkOrderFacility> page = wos.findPage(entity);

		Map<String, Object> map = Maps.newHashMap();
		map.put("total", page.getTotal());
		map.put("rows", page.getResult());

		return map;
	}

	@Path("get")
	@GET
	public Object get(@QueryParam("id") String id) {
		return wos.get(id);
	}

	@Path("create")
	@POST
	public ResultInfo create(WorkOrderFacility entity) {
		entity.setCreator(UserUtils.getUser());
		entity.setType("02");
		wos.create(entity);
		return ResultInfo.ok();
	}

	@Path("distribute")
	@POST
	public ResultInfo distribute(WorkOrderFacility entity) {
		wos.distribute(entity);
		return ResultInfo.ok();
	}

	@Path("process")
	@POST
	public ResultInfo process(WorkOrderFacility entity) {
		wos.process(entity, UserUtils.getUser());
		return ResultInfo.ok();
	}

	@Path("back")
	@POST
	public ResultInfo back(WorkOrderFacility entity) {
		wos.back(entity);
		return ResultInfo.ok();
	}

	@Path("toAudit")
	@POST
	public ResultInfo toAudit(WorkOrderFacility entity) {
		wos.toAudit(entity, UserUtils.getUser());
		return ResultInfo.ok();
	}

	@Path("audit")
	@POST
	public ResultInfo audit(WorkOrderFacility entity) {
		wos.audit(entity);
		return ResultInfo.ok();
	}

}
